<?php defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Description: 设备管理
 *
 * @author      Eden
 * @datetime    2016/12/13 00:53
 * @copyright   Beijing CmsTop Technology Co.,Ltd.
 */

class Device_Model extends ADM_Model
{

    /**
     * 初始化
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 添加设备
     * @param $params
     * @return array|bool
     * @throws Exception
     */
    public function doAddDevice($params)
    {
        try {
            $this->db->insert('stream_device', $params);
            if ($insertId = $this->db->insert_id()) {
                return array_merge($params, array('id' => $insertId));
            }
            return false;
        } catch(PDOException $e) {
            throw new Exception('设备添加失败');
        }
    }

    /**
     * 获取列表
     * @param int $offset
     * @param int $limit
     * @param string $keyword
     * @return array
     */
    public function getLists($offset = 0, $limit = 30, $keyword = '')
    {
        $return = array(
            'total' => 0,
            'nextPage' => false,
            "data" => []
        );
        $db = clone($this->db);
        if (!empty($keyword)) {
            $db->like('deviceName', $keyword);
            $db->or_like('deviceSerial', $keyword);
        }
        $total = $db->count_all_results('stream_device');
        if ($total == 0) {
            return $return;
        }

        $selectDb = clone($this->db);
        if (!empty($keyword)) {
            $selectDb->like('deviceName', $keyword);
            $selectDb->or_like('deviceSerial', $keyword);
        }
        $selectDb->order_by('id', 'DESC');
        $selectDb->limit($limit, $offset);
        $q = $selectDb->get('stream_device');
        $res = $q->result_array();
        if (!$res) {
            return $return;
        }

        $return['total'] = $total;
        $return['nextPage'] = ($offset + $limit) < $total;
        $return['data'] = $res;
        return $return;
    }

    /**
     * @return bool 获取所以在线的列表
     */
    public function getAllLists()
    {
        $selectDb = clone($this->db);
        $selectDb->order_by('id', 'DESC');
        $selectDb->where(array('status' => 1));
        $q = $selectDb->get('stream_device');
        return $q->result_array();
    }

    /**
     * 获取接口详情
     * @param $deviceId
     * @return bool
     */
    public function getDetail($deviceId)
    {

        $q = $this->db->get_where('stream_device', array('id' => $deviceId));
        $row = $q->row_array();
        if (empty($row)) {
            return false;
        }
        return $row;
    }

    /**
     * 编辑设备信息
     * @param $params
     * @return bool
     */
    public function editDevice($params)
    {
        $cloneDb = clone($this->db);
        $q = $cloneDb->get_where('stream_device', array('id' => $params['deviceId']));
        $r = $q->row_array();
        if (empty($r)) {
            $this->setError('无效设备');
            return false;
        }
        $deviceName = $params['deviceName'];
        $oldDeviceName = $r['deviceName'];

        // 修改设备名称
        $modifyName = false;
        if ($deviceName != $oldDeviceName) {
            if ($this->_modifyDeviceName($r['deviceSerial'], $deviceName)) {
                $modifyName = true;
            }
        }

        $data = array(
            'videoUrlHD' => value($params, 'videoUrlHD'),
            'videoUrlSD' => value($params, 'videoUrlSD'),
            'groupId' => value($params, 'groupId', 1)
        );

        $data = $modifyName ? array_merge($data, array('deviceName' => $params['deviceName'])) : $data;
        $this->db->where('id', $params['deviceId']);
        $this->db->update('stream_device', $data);
        return $this->db->affected_rows();
    }

    /**
     * 修改远程设备名
     * @param $deviceSerial
     * @param $deviceName
     * @return bool
     */
    private function _modifyDeviceName($deviceSerial, $deviceName)
    {
        $this->load->library('util/Util_Ys', null, 'ys');
        if ($this->ys->deviceEditName($deviceSerial, $deviceName)) {
            return true;
        }
        return false;
    }

}
